package com.hb.trade.api.manager.controller;

import com.hb.core.pojo.center.au.dto.AgencyUserBidDTO;
import com.hb.core.pojo.center.au.entity.User;
import com.hb.core.pojo.center.au.vo.AgencyBidUserVo;
import com.hb.core.pojo.enums.SmsTypeEnum;
import com.hb.core.pojo.enums.UserStatusEnum;
import com.hb.core.pojo.parent.PageResult;
import com.hb.core.pojo.project.sms.dto.SmsSendLogDTO;
import com.hb.trade.client.center.UserClient;
import com.hb.trade.client.project.SmsSendLogClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.msg.ResponseDto;
import org.fzzn.core.third.sms.service.SmsService;
import org.fzzn.core.util.ResultUtil;
import org.fzzn.core.web.anno.Version;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * describe:
 *
 * @author zhongdl
 * @date 2022/03/23
 */
@Slf4j
@Version(1.0)
@Api(tags = {"代理机构审核相关接口"})
@RequestMapping("/{version}")
@RestController
public class ApiAgencyUserController {


    protected final UserClient userClient;
    protected final SmsService smsService;
    protected final SmsSendLogClient smsSendLogClient;
    public ApiAgencyUserController(UserClient userClient, SmsService smsService, SmsSendLogClient smsSendLogClient) {
        this.userClient = userClient;
        this.smsService = smsService;
        this.smsSendLogClient = smsSendLogClient;
    }


    @ApiOperation(value = "代理机构管理列表接口", notes = "代理机构管理列表接口", httpMethod = "POST")
    @PostMapping("/agencyUser/pageAgencyUser")
    public ResponseDto<?> pageAgencyUser(@RequestBody AgencyUserBidDTO agencyUserDTO) {
        agencyUserDTO.setType(1);
        Message<PageResult<AgencyBidUserVo>> voPageResult = userClient.pageAuditUser(agencyUserDTO);
        return ResultUtil.msg(voPageResult);
    }


    @ApiOperation(value = "代理机构管理获得审核详情", notes = "代理机构管理获得审核详情", httpMethod = "POST")
    @PostMapping("/agencyUser/getAgencyBidUser")
    public ResponseDto getAgencyBidUser(@RequestBody AgencyUserBidDTO agencyUserDTO) {
        Message<AgencyBidUserVo> agencyBidUserVoMessage = userClient.getAgencyBidUser(agencyUserDTO);
        return ResultUtil.msg(agencyBidUserVoMessage);
    }

    @ApiOperation(value = "审核通过", notes = "审核通过", httpMethod = "POST")
    @PostMapping("/agencyUser/approved")
    public ResponseDto approved(@RequestBody AgencyUserBidDTO agencyUserDTO) {
        agencyUserDTO.setUserStatus(UserStatusEnum.APPROVED.getCode());
        Message<Boolean> agencyBidUserVoMessage = userClient.examine(agencyUserDTO);
        if (agencyBidUserVoMessage.getCode() == 200) {
            User user = userClient.getByUserId(agencyUserDTO.getId()).getData();
            boolean sendSms = smsService.sendSms(user.getPhone(), SmsTypeEnum.REGISTER.getTemplate(), null);
            // 短信日志记录
            SmsSendLogDTO smsSendLogDTO = new SmsSendLogDTO();
            smsSendLogDTO.setSendName(user.getUsername());
            smsSendLogDTO.setSendPhone(user.getPhone());
            smsSendLogDTO.setSendModul(SmsTypeEnum.REGISTER.getShowName());
            smsSendLogDTO.setSendTemplate(SmsTypeEnum.REGISTER.getTemplate());
            smsSendLogDTO.setSendStatus(sendSms ? 1 : 2);
            smsSendLogDTO.setCreateTime(LocalDateTime.now());
            smsSendLogClient.save(smsSendLogDTO);
        }
        return ResultUtil.msg(agencyBidUserVoMessage);
    }

    @ApiOperation(value = "审核不通过", notes = "审核不通过", httpMethod = "POST")
    @PostMapping("/agencyUser/notApproved")
    public ResponseDto notApproved(@RequestBody AgencyUserBidDTO agencyUserDTO) {
        agencyUserDTO.setUserStatus(UserStatusEnum.REJECT.getCode());

        Message<Boolean> agencyBidUserVoMessage = userClient.examine(agencyUserDTO);

        return ResultUtil.msg(agencyBidUserVoMessage);
    }


}
